package com.repair.dao.LineStation.impl;



import java.util.ArrayList;
import java.util.List;

import javassist.bytecode.analysis.Type;

import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;


import org.hibernate.type.StandardBasicTypes;
import com.repair.dao.LineStation.ILineStationDao;
import com.repair.domain.LineStation;
import com.repair.factory.HibernateSessionFactory;




public class LineStationDao implements ILineStationDao {

	@Override
	public void saveLineStation(LineStation obj) {
		Session session = HibernateSessionFactory.getSession();
		
		try{
			Transaction tx = session.beginTransaction();
			if(session != null){
				session.saveOrUpdate(obj);
			    tx.commit();
				session.flush();
				session.clear();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			if(session != null){
				session.flush();
				session.clear();
			}
		}
	}
	@Override
	@SuppressWarnings("unchecked")
	public List<LineStation> getAllLineStation(){
		List<LineStation> list = new ArrayList<LineStation>();
		Session session = HibernateSessionFactory.getSession();
		try{
			if(session != null){
				String sql = "select * from LINE_STATION";
				SQLQuery query = session.createSQLQuery(sql);
				list = query.addEntity(LineStation.class).list();
			}
		}
		catch(Exception e){
			
		}
		return list;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List getLineName(){
		List list = new ArrayList();
		Session session = HibernateSessionFactory.getSession();
		try{
			if(session != null){
				String sql = "select distinct line_number,line_name from LINE_STATION";
				SQLQuery query = session.createSQLQuery(sql);
				list = query.addScalar("line_number", StandardBasicTypes.STRING).addScalar("line_name", StandardBasicTypes.STRING).list();
			}
		}
		catch(Exception e){
			
		}
		return list;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List getStationName(String linenumber){
		List<LineStation> list = new ArrayList<LineStation>();
		Session session = HibernateSessionFactory.getSession();
		try{
			if(session != null){
				String sql = "select distinct station_number,station_name from LINE_STATION where line_number = '" + linenumber + "'";
				SQLQuery query = session.createSQLQuery(sql);
				list = query.addScalar("station_number",StandardBasicTypes.STRING).addScalar("station_name",  StandardBasicTypes.STRING).list();
			}
		}
		catch(Exception e){
			
		}
		return list;
	}
	
	
	@Override
	public void deleteLineStation(LineStation obj) {
		Session session = HibernateSessionFactory.getSession();
		try {
			Transaction tx = session.beginTransaction();
			if (session != null) {
				 session.delete(obj);
				tx.commit();
			}
		} catch (Exception e) {

		} finally {
			if (session != null) {
				session.flush();
				session.clear();
			}
		}

	}
}
